//
//  YDChoosePictureView.swift
//  weibo
//
//  Created by 孙亚东 on 16/6/30.
//  Copyright © 2016年 Sunyadong. All rights reserved.
//

import UIKit

let itemMargin: CGFloat = 5.0
let itemWH: CGFloat = (screenWidth - 2 * itemMargin - 20) / 3

let YDChoosePictureViewIdentify = "YDChoosePictureViewIdentify"

class YDChoosePictureView: UICollectionView, YDChooseViewCellProtocol,UICollectionViewDelegate{
    
    
    //定义一个闭包
    var closure = { ()->() in

    
    }
    
    
    
    //定义一个数组，存放图片
   private var chooseImgArr: [UIImage] = [UIImage]()
    
    //  创建一个方法，将图片添加到数组中
    
    func addImage (img: UIImage) {
    
        self.chooseImgArr.append(img)
        
        self.reloadData()
    
    }
    
    
    //创建一个方法，判断是否大于9张
    
    func isPresent()->Bool {
        
        print(self.chooseImgArr.count)
    
        return self.chooseImgArr.count > 8 ? true:false
    
    
    }
    
    
    
    //创建一个layout
    
    let flow = UICollectionViewFlowLayout()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flow)
        
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
        
    }
    
    
    func setupUI() {
    
        backgroundColor = UIColor.blueColor()
        
        //设置cell的frame
        flow.itemSize = CGSizeMake(itemWH, itemWH)
        flow.minimumInteritemSpacing = itemMargin
        flow.minimumLineSpacing = itemMargin
        
        
        //设置数据源代理
        self.dataSource = self
        self.delegate = self
        
        
        //注册collectionCell
        self.registerClass(YDChooseViewCell.self, forCellWithReuseIdentifier: YDChoosePictureViewIdentify)
        
    
    
    }
    

}
//实现数据源方法
extension YDChoosePictureView: UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if self.chooseImgArr.count == 0 {
        
            return 0
        
        
        }else if self.chooseImgArr.count == 9{
        
            return 9
        
        
        }else {
        
             return self.chooseImgArr.count + 1
            
        }
        
        
    }
   
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YDChoosePictureViewIdentify, forIndexPath: indexPath) as! YDChooseViewCell
        

        
        cell.delegate = self
        
        if indexPath.item == chooseImgArr.count {
        
            cell.chooseImage = UIImage(named: "compose_pic_add")
            
            //隐藏删除按钮
            cell.deleBtn.hidden = true
        
        
        }else {
        
            cell.chooseImage = chooseImgArr[indexPath.item]
            cell.index = indexPath
            
            cell.deleBtn.hidden = false
            
        }
        
        return cell
        
        
    }
    
    //实现collection的代理方法
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.item == self.chooseImgArr.count {
            
        
            //执行父类的点击方法
            self.closure()
            
         
        
        }
        
      
    }
    
    
    //实现代理方法
    
    func deleteImage(index: NSIndexPath) {
        
        self.chooseImgArr.removeAtIndex(index.item)
        
        reloadData()
        
    }
    

}







//自定义cell

//用代理实现删除数据源

protocol YDChooseViewCellProtocol: NSObjectProtocol{
    //代理方法
    func deleteImage (index: NSIndexPath)
    
}



class YDChooseViewCell: UICollectionViewCell {
    
    
    //定义一个代理属性
    var delegate : YDChooseViewCellProtocol?
    
    //定义一个indexpath的属性，用于判断删除了那个图片
    var index: NSIndexPath?
    
    //定义一个属性，接收image
    var chooseImage: UIImage?  {
    
        didSet {
        
        
            pictureImg.image = chooseImage
        
  
        }
    
    
    
    }
    
    


    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func  setupUI() {
    
        contentView.addSubview(pictureImg)
    
    //设置约束
        pictureImg.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
            
            
        }
        
        
        contentView.addSubview(deleBtn)
        
        deleBtn.snp_makeConstraints { (make) -> Void in
            
            make.right.top.equalTo(contentView)
            
            
        }
    
    
    }
    
    
    //懒加载控件
    
    private lazy var pictureImg: UIImageView = {
    
        let imgView = UIImageView()
        
        return imgView
 
    }()
    
    
    lazy var deleBtn: UIButton = {
    
    
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "compose_photo_close"), forState: .Normal)

        
        btn.addTarget(self, action: "deleBtnClick", forControlEvents: .TouchUpInside)
        
        btn.sizeToFit()
        
        return btn
 
    
    }()
    
    //实现按钮的点击方法
    func deleBtnClick () {
        
        //删除数据源，reloadData
        self.delegate?.deleteImage(self.index!)
        
        
    }


}



